<template>
  <div class="result-container">
    <van-list
      v-model="resultLoading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="(searchresult, index) in searchResultLists" :key="index" :title="searchresult.title" />
    </van-list>
  </div>
</template>
<script>
import { searchResults } from '@/api/search'
export default {
  name: 'searchResult',
  data () {
    return {
      searchResultLists: [],
      resultLoading: false,
      finished: false,
      page: 1, // 页码
      perPage: 20 // 每页大小
    }
  },
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  methods: {
    async onLoad () {
      // 请求获取数据
      const res = await searchResults({
        page: this.page,
        per_page: this.perPage,
        q: this.searchText
      })
      // 添加数据至列表
      this.searchResultLists.push(...res.data.data.results)
      // 改变loading状态
      this.resultLoading = false
      // 当无数据时, 改变Finnish状态
      if (res.data.data.results.length) {
        // 如果有，则更新获取下一页数据的页码
        this.page++
      } else {
        // 如果没有，则把 finished 设置为 true，关闭加载更多
        this.finished = true
      }
    }
  }
}
</script>
<style lang="less">
.result-container{
  position: fixed;
  left: 0;
  right: 0;
  top: 54px;
  bottom: 0;
  overflow-y: auto;
}
</style>
